<?php

namespace app\home\controller;

use app\common\validate\merchant\MerchantValid;
use app\common\validate\virtual\VirtualPromoValid;
use app\home\loader\Services;
use think\App;
use think\Exception;

/**
 * 导购模块
 */
class ShopSalesman extends ApiBase
{

    /**
     * 商户入库
     * mer_id  主键
     */
    public function merchant_index(): void
    {
        try {
            $mer_id = $this->shopUser['mer_id'];
            $info = Services::Merchant()->getUserInfo($mer_id);

            api_result(API_SUCCESS, '请求成功', $info);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 扫码获取商品活动
     * product_code 扫码商品code
     */
    public function product_prom(): void
    {
        try {
            $data = input('get.');
            validate(VirtualPromoValid::class)->scene('product_prom')->check($data);
            $market_data = Services::VirtualPromo()->getProductActivity(
                $this->shopUser['mer_id'],
                $data['product_code'],
                $this->shopUser['open_id'],
                $data['lng'],
                $data['lat'], $this->shopUser['user_id']);
            api_result(API_SUCCESS, '请求成功', $market_data);
        } catch (Exception $e) {
            api_result($e);
        }
    }


    /**
     * 获取混合活动列表
     */
    public function prom_list(): void
    {
        try {
            $data = input('get.');
            validate(VirtualPromoValid::class)->scene('prom_list')->check($data);
            $market_data = Services::VirtualPromo()->getActivity(
                $this->shopUser['mer_id'],
                $data['product_code'],
                $this->shopUser['user_id']
            );
            api_result(API_SUCCESS, '请求成功', $market_data);
        } catch (Exception $e) {
            api_result($e);
        }
    }


    /**
     * 虚码活动详情选择
     */
    public function prom_info(): void
    {
        try {
            $data = input('get.');
            validate(VirtualPromoValid::class)->scene('prom_list')->check($data);
            $market_data = Services::VirtualPromo()->getActivityInfo($this->shopUser['mer_id'], $data['vp_id'], $data['product_code'], $this->shopUser['user_id']);
            api_result(API_SUCCESS, '请求成功', $market_data);
        } catch (Exception $e) {
            api_result($e);
        }
    }


    /**
     * 扫码活动提交
     */
    public function submit_activity(): void
    {
        try {
            $data = input('post.');
            validate(MerchantValid::class)->scene('submit_activity')->check($data);
            //商户mer_id , 扫码记录编号order_no， ticket 小票信息
            $market_data = Services::VirtualPromo()->submitActivitys(
                $this->shopUser['mer_id'],
                $data['order_no'],
                $this->shopUser['user_id'],
                $data['ticket']
            );
            api_result(API_SUCCESS, '请求成功', $market_data);
        } catch (Exception $e) {
            api_result($e);
        }
    }


    /**
     * 销售
     */
    public function get_virtual_market(): void
    {
        try {
            $data = input('get.');

            $where[] = ['mer_id','=' , $this->shopUser['mer_id']];
            if (!empty($data['type'])){
                if ((int)$data['type'] === 2){
                    $where[] = ['shop_id', '=', $this->shopUser['shop_id']];
                }else {
                    $where[] = ['salesman_id','=' , $this->shopUser['user_id']];
                }
            }

            $market_data = Services::VirtualPromo()->getMarket(
                $where,
                $data['page'] ?: 1,
                $data['page_size'] ?: 30
            );
            api_result(API_SUCCESS, '请求成功', $market_data);
        } catch (Exception $e) {
            api_result($e);
        }
    }


}
